package handler

import (
	v1 "blog/api/v1"
	"blog/internal/service"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type ArticleHandler struct {
	*Handler
	articleService service.ArticleService
}

func NewArticleHandler(
	handler *Handler,
	articleService service.ArticleService,
) *ArticleHandler {
	return &ArticleHandler{
		Handler:        handler,
		articleService: articleService,
	}
}
func (h *ArticleHandler) CreateArticle(ctx *gin.Context) {
	var req v1.CreateArticleRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	if err := h.articleService.CreateArticle(ctx, req.Title, req.Content, req.AuthorID, req.TagIDs); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *ArticleHandler) ListArticles(ctx *gin.Context) {
	articles, err := h.articleService.ListArticles(ctx)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, articles)
}

func (h *ArticleHandler) ListArticlesByAuthorID(ctx *gin.Context) {
	authorIDStr := ctx.Param("id")
	authorID, err := strconv.ParseUint(authorIDStr, 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	articles, err := h.articleService.ListArticlesByAuthorID(ctx, uint(authorID))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, articles)
}

func (h *ArticleHandler) UpdateArticle(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		fmt.Println("参数绑定失败:", err)
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	usertype := GetUserTypeFromCtx(ctx)
	userId := GetUserIdFromCtx(ctx)

	var req v1.UpdateArticleRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}

	if err := h.articleService.UpdateArticle(ctx, uint(id), req.Title, req.Content, req.TagIDs, userId, usertype); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *ArticleHandler) DeleteArticle(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	usertype := GetUserTypeFromCtx(ctx)
	userId := GetUserIdFromCtx(ctx)
	if err := h.articleService.DeleteArticleByArticleID(ctx, uint(id), userId, usertype); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *ArticleHandler) ListArticlesByTagID(ctx *gin.Context) {
	tagIDStr := ctx.Param("id")
	tagID, err := strconv.ParseUint(tagIDStr, 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}

	result, err := h.articleService.ListArticlesByTagID(ctx, uint(tagID))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}

	v1.HandleSuccess(ctx, result)
}

// 根据标题模糊查询文章列表
func (h *ArticleHandler) SearchArticlesByTitle(ctx *gin.Context) {
	keyword := ctx.Query("keyword")
	if keyword == "" {
		v1.HandleError(ctx, http.StatusBadRequest, errors.New("查询关键词不能为空"), nil)
		return
	}

	result, err := h.articleService.SearchArticlesByTitle(ctx, keyword)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}

	v1.HandleSuccess(ctx, result)
}
func (h *ArticleHandler) GetArticleByID(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}

	article, err := h.articleService.GetArticleByID(ctx, uint(id))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}

	v1.HandleSuccess(ctx, article)
}
